import { EPockerStatus, ESuit } from "../../ConfigEnum";
import { Pocker } from "../../GameScene/GameDB";

const {ccclass, property} = cc._decorator;

const POINT_MAP = {
  "1":"1",
  "2":"2",
  "3":"3",
  "4":"4",
  "5":"5",
  "6":"6",
  "7":"7",
  "8":"8",
  "9":"9",
  "10":"10",
  "11":"J",
  "12":"Q",
  "13":"K",
}

@ccclass
export default class UiPocker extends cc.Component {

    @property(cc.Sprite) bgSprite:cc.Sprite = null;
    @property(cc.Sprite) bigSuitSprite: cc.Sprite = null;
    @property(cc.Sprite) smallSuitSprite: cc.Sprite = null;
    @property(cc.Label) pointLabel: cc.Label = null;
    @property(cc.SpriteFrame) texFrontBG: cc.SpriteFrame = null;
    @property(cc.SpriteFrame) texBackBG: cc.SpriteFrame = null;
    @property([cc.SpriteFrame]) bigSuits:cc.SpriteFrame[] = [];
    @property([cc.SpriteFrame]) smallSuits:cc.SpriteFrame[] = [];
    @property([cc.SpriteFrame]) texFaces:cc.SpriteFrame[] = [];

    private redTextColor:cc.Color = cc.color(183,24,40);
    private blackTextColor:cc.Color = cc.Color.BLACK;

    onLoad(){}

    start () {}

    init(pocker:Pocker){
      pocker.bind(this);
      this.pointLabel.string = `${POINT_MAP[pocker.point]}`;
      this.pointLabel.node.color = 
        (pocker.suit==ESuit.HeiTao || pocker.suit==ESuit.MeiHua) 
          ? this.blackTextColor
          : this.redTextColor;
      
      if(pocker.point>=11){
        this.bigSuitSprite.spriteFrame = this.texFaces[pocker.point-11];
      }else{
        this.bigSuitSprite.spriteFrame = this.bigSuits[pocker.suit];
      }
      this.smallSuitSprite.spriteFrame = this.smallSuits[pocker.suit];
      this.setStatus(pocker.ePockerStatus);
    }

    refresh(){
      this.setStatus(EPockerStatus.OPEN);
    }

    setStatus(ePockerStatus:EPockerStatus){
      if(ePockerStatus==EPockerStatus.CLOSE){
        this.bgSprite.spriteFrame = this.texBackBG;
        this.bigSuitSprite.node.active = false;
        this.pointLabel.node.active = false;
        this.smallSuitSprite.node.active = false;
      }else{
        this.bgSprite.spriteFrame = this.texFrontBG;
        this.bigSuitSprite.node.active = true;
        this.pointLabel.node.active = true;
        this.smallSuitSprite.node.active = true;
      }
    }
}